package net.edu_soft.ec.asset.controller;

import com.alibaba.fastjson.JSONObject;
import net.edu_soft.ec.asset.constant.EduConstant;
import net.edu_soft.ec.asset.pojo.*;
import net.edu_soft.ec.asset.pojo.vo.ProductVO;
import net.edu_soft.ec.asset.pojo.vo.SchoolVO;
import net.edu_soft.ec.asset.service.ProductService;
import net.edu_soft.ec.asset.service.UserAffService;
import net.edu_soft.ec.common.util.PuingUtil;
import net.edu_soft.ec.common.util.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private UserAffService userAffService;

    /**
     * 根据产品id查询产品详细信息，包括详情与扩展
     * @param productId 产品id
     * @return 产品组合对象
     */
    @RequestMapping(value = "/getProductById")
    public ResultVo getProductById(int productId){
        return ResultVo.succeed("成功获取信息",productService.findById(productId));
    }


    /**
     * 分页查询产品信息，根据关注的人数排序
     * @param pageNum 起始页
     * @param pageSize 记录数
     * @return 分页后产品集合
     */
    @RequestMapping(value = "/getProducts")
    public ResultVo getProducts(Integer pageNum, Integer pageSize){
        return ResultVo.succeed("成功获取信息",productService.getProducts(pageNum,pageSize));
    }


    /**
     * 根据产品分类分页查询商品信息
     * @param productTypeId 产品分类id
     * @param pageNum 当前页
     * @param pageSize 每页记录数
     * @return 产品集合
     */
    @RequestMapping(value = "/getProductsByTypeId")
    public ResultVo getProductsByTypeId(Integer productTypeId,Integer pageNum, Integer pageSize){
        return ResultVo.succeed("成功获取信息",productService.findAllByTypeId(productTypeId,pageNum,pageSize));
    }


    /**
     * 批量删除，是否确认删除
     * @param isDel 确认标记 0位否，1为是
     * @param ids 字符串形式id集合
     * @return 返回确认消息
     */
    @RequestMapping(value = "/delProductByIds")
    public ResultVo delProductByIds(Integer isDel,String ids){
        List<Integer> idList = PuingUtil.getNumber(ids);
        int i = productService.delProductByIds(isDel,idList);
        String m = i==0?"成功删除！！！":"删除失败！！！";
        return ResultVo.succeed(m,null);
    }

    /***
     * 添加产品
     * @param product 产品基础信息、产品详情、产品扩展
     * @return
     */
    @RequestMapping(value = "/saveProduct")
    public ResultVo saveProduct(Product product, ProductDetail productDetail, ProExtension proExtension,@RequestBody String labels, @RequestBody  String schools){
        //1.获取管理员的id及姓名
        Session session = SecurityUtils.getSubject().getSession();
        UserInfo u = (UserInfo) session.getAttribute("userInfo");
        Integer userInfoId = u.getUserInfoId();
        String userName = u.getUsername();

        ProductVO productVO = new ProductVO();
        productVO.setProduct(product);
        productVO.setProductDetail(productDetail);
        productVO.setProExtension(proExtension);
        productVO.getProExtension().setLabel(labels);
        //创建审核记录
        Audit audit = new Audit();
        audit.setUserInfoId(userInfoId);
        audit.setAuditor(userName);
        int i = productService.saveProduct(productVO,audit,schools);
        String m = i==1?"成功添加！！！":"添加失败！！！";
        return ResultVo.succeed(m,labels);
    }

    /***
     * 修改产品信息
     * @param product 产品基础信息
     * @param productDetail 产品详情
     * @param proExtension 产品扩展
     * @return
     */
    @RequestMapping(value = "/updateProduct")
    public ResultVo updateProduct(Product product, ProductDetail productDetail, ProExtension proExtension ,@RequestBody String labels, @RequestBody  String schools){
        ProductVO productVO = new ProductVO();
        productVO.setProduct(product);
        productVO.setProductDetail(productDetail);
        productVO.setProExtension(proExtension);
        productVO.getProExtension().setLabel(labels);
        if(schools!=null||!"".equals(schools)){
            //不为空则添加进
            productVO.setSchools(JSONObject.parseArray(schools, SchoolVO.class));
        }
        int i = productService.updateProduct(productVO);
        String m = i==1?"成功修改！！！":"修改失败！！！";
        return ResultVo.succeed(m,null);
    }


    /***
     * 关注或者取消产品、厂家、产品分类
     * @param subjectId 主体id：产品or厂家or产品分类
     * @param flag 1关注 0取消关注
     * @return 确认信息
     */
    @RequestMapping(value = "/attentionOrNo")
    public ResultVo attentionOrNo(Integer subjectId,Integer flag){
        //用户id
        int userId = 15;
        int i = userAffService.attentionOrNo(userId, subjectId, flag, EduConstant.USER_PRODUCT);
        String m = i==1?"关注修改！！！":"关注失败！！！";
        return ResultVo.succeed(m,null);
    }


    /***
     * 分页查询厂家的产品，最新收录、用户最多，关注最多
     * @param factoryName 厂家名
     * @param condition 1、用户最多、2最新收录
     * @param pageNum 当前页
     * @param pageSize 每页数
     * @return
     */
    @RequestMapping(value = "/findFacPro")
    public ResultVo findFacPro(String factoryName,Integer condition,Integer pageNum,Integer pageSize){
        return ResultVo.succeed("分页查询厂家的产品，最新收录、用户最多",productService.findFacPro(factoryName,condition,pageNum,pageSize));
    }

    /***
     * 按最新时间排序查询所有产品
     * @param condition 条件
     * @param pageNum 当前页
     * @param pageSize 每页数
     * @return
     */
    @RequestMapping(value = "/finProByDate")
    public ResultVo finProByDate(Integer condition,Integer pageNum,Integer pageSize){
        return ResultVo.succeed("按最新时间排序查询所有产品",productService.finProByDate(condition,pageNum,pageSize));
    }



    
}
